package nl.nickthissen.remindme.activities;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.activeandroid.query.Select;

import java.util.ArrayList;

import nl.nickthissen.remindme.R;
import nl.nickthissen.remindme.adapters.CategoryAdapter;
import nl.nickthissen.remindme.common.SaveResult;
import nl.nickthissen.remindme.data.Category;
import nl.nickthissen.remindme.data.CategoryManager;
import nl.nickthissen.remindme.data.ReminderItem;

/**
 * Created by nthissen on 11/06/13.
 */
public class EditReminderItemActivity extends EditActivity<ReminderItem>
{
    private EditText txtTitle, txtDescr;
    private LinearLayout categoryLayout;
    private Spinner spinner;
    private AlertDialog dialog;

    private ReminderItem item;
    private Category category;
    private ArrayList<Category> categories;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.edit_item);

        txtTitle = (EditText) this.findViewById(R.id.txtName);
        txtDescr = (EditText) this.findViewById(R.id.txtDescr);
        categoryLayout = (LinearLayout) this.findViewById(R.id.categoryLayout);
        spinner = (Spinner) this.findViewById(R.id.spinner);

        long id = this.getItemId();

        item = new Select().from(ReminderItem.class).where("Id=?", id).executeSingle();
        if (item == null) item = new ReminderItem();
        this.setCategory(item.category);

        txtTitle.setText(item.title);
        txtDescr.setText(item.description);

        categories = CategoryManager.categories;
        spinner.setAdapter(new CategoryAdapter(this, R.layout.category_row, categories));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                Category cat = categories.get(position);
                setCategory(cat);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
                spinner.setSelection(0);
            }
        });

        int catIndex = category == null ? 0 : categories.indexOf(category);
        if (catIndex >= 0 && catIndex < categories.size())
        {
            spinner.setSelection(catIndex);
        }
    }

    public static Intent getIntent(Context context, long id)
    {
        Intent i = new Intent(context, EditReminderItemActivity.class);
        i.putExtra(ITEM_ID, id);
        return i;
    }

    private void setCategory(Category category)
    {
        this.category = category;
        categoryLayout.setBackgroundColor(category.color);
    }

    @Override
    public SaveResult onSave()
    {
        Editable title = txtTitle.getText();
        Editable descr = txtDescr.getText();

        if (title == null || TextUtils.isEmpty(title))
        {
            return SaveResult.error("Title cannot be empty!");
        }

        item.title = title.toString();
        item.category = category;
        if (descr == null)
            item.description = "";
        else
            item.description = descr.toString();

        item.save();
        return SaveResult.ok();
    }

    @Override
    public void onFinished()
    {
        this.finish();
    }
}
